package org.jeecg.third.fangzheng;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.MD5Util;
import org.jeecg.modules.content.entity.ConContentTemplate;
import org.jeecgframework.p3.core.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class FangApiUtil {


    @Value("${fangZheng.channelCode}")
    private  String  channelCode;
    /**
     * 栏目树获取接口
     * @param url
     * @return
     */
    public  JSONArray getBanners(){
        String token = MD5Util.MD5Encode(channelCode+"founder","utf-8");
        String result = HttpUtil.get("https://preview.suzhou-news.cn/news/banners?token" + token);
        System.out.println(result);
        JSONObject  jsonObject = JSON.parseObject(result);

        return  jsonObject.getJSONArray("colunms");
    }



    /**
     * 稿件推送接口
     *  https://preview.suzhou-news.cn/news/syncAdd
     * @return
     */
    public  String syncAdd(ConContentTemplate conContentTemplate,String columId,String columName){
        String token = MD5Util.MD5Encode(conContentTemplate.getId()+ channelCode,"utf-8");
        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("sys_documentid",conContentTemplate.getId());
        paramMap.put("sys_authors",conContentTemplate.getAuthorName());
        paramMap.put("doc_author",conContentTemplate.getAuthorName());
        paramMap.put("doc_createtime",conContentTemplate.getCreateTime());
        paramMap.put("doc_content",conContentTemplate.getContent());
        paramMap.put("pub_column",columId);
        paramMap.put("pub_columnname",columName);
        paramMap.put("sys_topic",conContentTemplate.getContentTitle());
        paramMap.put("doc_keyword",conContentTemplate.getKeyword());

        paramMap.put("thumb",conContentTemplate.getPcLogoUrl());



        String result = HttpUtil.post("https://preview.suzhou-news.cn/news/syncAdd" + token,paramMap);
        System.out.println(result);


        return  result;
    }


    /**
     * 稿件推送接口
     *  https://preview.suzhou-news.cn/news/syncAdd
     *  {
     * "id":970,
     *  "doc_lib_id":145,
     * "doc_id":15497,
     * "op":"DEL",
     * "op_time":"2017-06-14 13:18:47"
     * }
     * @return
     */
    public  JSONObject updateStatus(String docId,String docLibId,String op){
        String token = MD5Util.MD5Encode(docId + channelCode,"utf-8");
        Map<String,Object> paramMap = new HashMap<>();

        //todo  id?
        paramMap.put("doc_id",docId);
        paramMap.put("doc_lib_id",docLibId);
        paramMap.put("op",op);
        paramMap.put("op_time", DateUtils.formatDateTime());




        String result = HttpUtil.post("https://preview.suzhou-news.cn/news/banners/syncUpdate" + token,paramMap);
        System.out.println(result);
        JSONObject  jsonObject = JSON.parseObject(result);

        return  jsonObject;
    }




    public static void main(String[] args) {
       // System.out.println( getBanners(""));
    }
}
